//
//  Drawer.c
//  Drawer
//
//  Created by Ji Hu on 12-9-3.
//  Copyright (c) 2012年 爱辅导. All rights reserved.
//

#include <stdio.h>
#include <math.h>
#include "Drawer.h"

CGPoint cMin(CGPoint *p,CGPoint *ve,CGFloat value){
    return CGPointMake(p->x-ve->x*value, p->y-ve->y*value);
}

//add value from point to ve direction
CGPoint cAdd(CGPoint *p,CGPoint *ve,CGFloat value){
    return CGPointMake(p->x+ve->x*value, p->y+ve->y*value);
}

CGPoint cPerpendicularVec(CGPoint *p){
    CGFloat len = caculate2PowAnd2Sqrt(p->x, p->y);
    
    return CGPointMake(p->y/len, -p->x/len);
}

CGPoint direction(CGPoint from,CGPoint to){
    CGPoint d = CGPointMake(to.x-from.x,to.y-from.y);
    CGFloat len = caculate2PowAnd2Sqrt(d.x, d.y);
    
    return CGPointMake(d.x/len, d.y/len);
}

void outRect(CGRect *rect,float offset){
    
    rect->origin.x = rect->origin.x - offset;
    rect->origin.y = rect->origin.y - offset;
    rect->size.width = rect->size.width + 2*offset;
    rect->size.height = rect->size.height + 2*offset;
    
}

float caculate2PowAnd2Sqrt(float x,float y){
    return sqrtf(x*x+y*y);
}

float filter(float v,float min,float max){
    return v<min?min:(v<max?v:max);
}

float caculateDistance(CGPoint *p1,CGPoint *p2){
    float dx = p1->x - p2->x;
    float dy = p1->y - p2->y;
    return caculate2PowAnd2Sqrt(dx, dy);
}